import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    goodsList: [],
    totalNum: 0,
  },
  mutations: {
    joinCar(state, params) {
      const index = state.goodsList.findIndex(item => item.id === params.id);
      state.totalNum += params.num;
      if (index !== -1) {
        state.goodsList[index].num += params.num;
        return;
      }
      state.goodsList.push(params);
    },
    delCarGood(state, id) {
      const i = state.goodsList.findIndex(item => item.id === id);
      state.totalNum = state.totalNum - state.goodsList[i].num;
      state.goodsList.splice(i, 1);
    },
    add(state, id) {
      state.goodsList.forEach(item => {
        if (item.id === id) {
          item.num++;
          state.totalNum++;
        };
      })
    },
    sub(state, id) {
      state.goodsList.forEach(item => {
        if (item.id === id) {
          item.num--;
          state.totalNum--;
        }
      })
    },
  },
  actions: {
    
  },
  getters: {

  },
  modules: {

  }
})
